import { defineStore } from 'pinia'
import request from '@/utils/request'

// 定义报销接口
interface Expense {
  id: number
  admin_id: number
  did: number
  expense_time: string
  income_month: string
  amount: number
  cate_id: number
  remarks: string
  check_status: number
  check_uids: string
  check_time: number
  pay_status: number
  pay_admin_id: number
  pay_time: number
  file_ids: string
  create_time: number
  update_time: number
  delete_time: number
  // 扩展字段
  check_status_str?: string
  admin_name?: string
  department?: string
  check_user?: string
  pay_name?: string
}

// 定义发票接口
interface Invoice {
  id: number
  admin_id: number
  did: number
  contract_id: number
  project_id: number
  customer_id: number
  name: string
  code: string
  amount: number
  tax_amount: number
  no_tax_amount: number
  check_status: number
  check_uids: string
  check_time: number
  open_status: number
  open_admin_id: number
  open_time: number
  enter_time: number
  delivery: number
  file_ids: string
  other_file_ids: string
  create_time: number
  update_time: number
  delete_time: number
  // 扩展字段
  check_status_str?: string
  admin_name?: string
  department?: string
  contract_name?: string
  open_name?: string
}

// 定义收票接口
interface Ticket {
  id: number
  admin_id: number
  did: number
  supplier_id: number
  purchase_id: number
  project_id: number
  name: string
  code: string
  amount: number
  tax_amount: number
  no_tax_amount: number
  check_status: number
  check_uids: string
  check_time: number
  open_status: number
  open_time: number
  pay_status: number
  pay_time: number
  file_ids: string
  other_file_ids: string
  create_time: number
  update_time: number
  delete_time: number
  // 扩展字段
  check_status_str?: string
  admin_name?: string
  department?: string
  supplier_name?: string
  open_time_str?: string
  pay_time_str?: string
}

// 定义费用分类接口
interface ExpenseCate {
  id: number
  title: string
  status: number
  create_time: number
}

// 定义成本分类接口
interface CostCate {
  id: number
  title: string
  status: number
  create_time: number
}

// 定义API响应类型
interface ApiResponse<T> {
  code: number
  msg: string
  data: T
  count: number
}

interface FinanceState {
  expenses: Expense[]
  expenseTotal: number
  invoices: Invoice[]
  invoiceTotal: number
  tickets: Ticket[]
  ticketTotal: number
  expenseCates: ExpenseCate[]
  costCates: CostCate[]
}

// 财务相关API
const financeApi = {
  // 获取报销列表
  getExpenseList: (params: any): Promise<ApiResponse<Expense[]>> => {
    return request({
      url: '/api/finance/expense/datalist',
      method: 'get',
      params,
    })
  },

  // 获取发票列表
  getInvoiceList: (params: any): Promise<ApiResponse<Invoice[]>> => {
    return request({
      url: '/api/finance/invoice/datalist',
      method: 'get',
      params,
    })
  },

  // 获取收票列表
  getTicketList: (params: any): Promise<ApiResponse<Ticket[]>> => {
    return request({
      url: '/api/finance/ticket/datalist',
      method: 'get',
      params,
    })
  },

  // 删除报销项
  delExpenseInterfix: (data: any) => {
    return request({
      url: '/api/finance/del_expense_interfix',
      method: 'post',
      data,
    })
  },

  // 报销设置为已打款
  setExpensePay: (data: any) => {
    return request({
      url: '/api/finance/topay',
      method: 'post',
      data,
    })
  },

  // 开具发票
  openInvoice: (data: any) => {
    return request({
      url: '/api/finance/open',
      method: 'post',
      data,
    })
  },

  // 作废发票
  voidInvoice: (data: any) => {
    return request({
      url: '/api/finance/tovoid',
      method: 'post',
      data,
    })
  },

  // 反作废发票
  unvoidInvoice: (data: any) => {
    return request({
      url: '/api/finance/novoid',
      method: 'post',
      data,
    })
  },

  // 上传电子发票
  uploadInvoice: (data: any) => {
    return request({
      url: '/api/finance/upload_invoice',
      method: 'post',
      data,
    })
  },

  // 作废收票
  voidTicket: (data: any) => {
    return request({
      url: '/api/finance/tovoid_ticket',
      method: 'post',
      data,
    })
  },

  // 反作废收票
  unvoidTicket: (data: any) => {
    return request({
      url: '/api/finance/novoid_ticket',
      method: 'post',
      data,
    })
  },

  // 上传收票
  uploadTicket: (data: any) => {
    return request({
      url: '/api/finance/upload_ticket',
      method: 'post',
      data,
    })
  },
}

export const useFinanceStore = defineStore('finance', {
  state: (): FinanceState => ({
    expenses: [],
    expenseTotal: 0,
    invoices: [],
    invoiceTotal: 0,
    tickets: [],
    ticketTotal: 0,
    expenseCates: [],
    costCates: [],
  }),

  actions: {
    // 获取报销列表
    async getExpenseListAction(params: any) {
      try {
        const res = await financeApi.getExpenseList(params)
        if (res.code === 0) {
          this.expenses = res.data
          this.expenseTotal = res.count
        }
        return res
      } catch (error) {
        throw error
      }
    },

    // 获取发票列表
    async getInvoiceListAction(params: any) {
      try {
        const res = await financeApi.getInvoiceList(params)
        if (res.code === 0) {
          this.invoices = res.data
          this.invoiceTotal = res.count
        }
        return res
      } catch (error) {
        throw error
      }
    },

    // 获取收票列表
    async getTicketListAction(params: any) {
      try {
        const res = await financeApi.getTicketList(params)
        if (res.code === 0) {
          this.tickets = res.data
          this.ticketTotal = res.count
        }
        return res
      } catch (error) {
        throw error
      }
    },

    // 删除报销项
    async delExpenseInterfixAction(data: any) {
      try {
        const res = await financeApi.delExpenseInterfix(data)
        return res
      } catch (error) {
        throw error
      }
    },

    // 报销设置为已打款
    async setExpensePayAction(data: any) {
      try {
        const res = await financeApi.setExpensePay(data)
        return res
      } catch (error) {
        throw error
      }
    },

    // 开具发票
    async openInvoiceAction(data: any) {
      try {
        const res = await financeApi.openInvoice(data)
        return res
      } catch (error) {
        throw error
      }
    },

    // 作废发票
    async voidInvoiceAction(data: any) {
      try {
        const res = await financeApi.voidInvoice(data)
        return res
      } catch (error) {
        throw error
      }
    },

    // 反作废发票
    async unvoidInvoiceAction(data: any) {
      try {
        const res = await financeApi.unvoidInvoice(data)
        return res
      } catch (error) {
        throw error
      }
    },

    // 上传电子发票
    async uploadInvoiceAction(data: any) {
      try {
        const res = await financeApi.uploadInvoice(data)
        return res
      } catch (error) {
        throw error
      }
    },

    // 作废收票
    async voidTicketAction(data: any) {
      try {
        const res = await financeApi.voidTicket(data)
        return res
      } catch (error) {
        throw error
      }
    },

    // 反作废收票
    async unvoidTicketAction(data: any) {
      try {
        const res = await financeApi.unvoidTicket(data)
        return res
      } catch (error) {
        throw error
      }
    },

    // 上传收票
    async uploadTicketAction(data: any) {
      try {
        const res = await financeApi.uploadTicket(data)
        return res
      } catch (error) {
        throw error
      }
    },
  },
})
